﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PayrollHR.Web;
using Payroll.Common.Utils;
using Payroll.BusinessLogic.Utils;
using Payroll.BusinessLogic.EmployeeManagement;
using Payroll.BusinessLogic;

namespace PayrollHR.Administration.Modules
{
    public partial class Employees : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }
        private void Bind()
        {
            var result = this.EmployeeService.GetAllEmployee(Name: ctrEmployeeSearch.EmployeeName, username: ctrEmployeeSearch.UserName, pageIndex: 0, pageSize: int.MaxValue);            
            gvEmployees.DataSource = result;
            gvEmployees.DataBind();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    List<int> employeeSelect = new List<int> { };
                    foreach (GridViewRow items in gvEmployees.Rows)
                    {
                        CheckBox employeeCheck = items.FindControl("cbEmployeeInfo") as CheckBox;
                        HiddenField employeeIdHidden = items.FindControl("hfEmployeeID") as HiddenField;
                        int eID = Convert.ToInt32(employeeIdHidden.Value);
                        if (eID == 1)
                            continue;
                        if (employeeCheck.Checked)
                        {
                            employeeSelect.Add(eID);
                        }
                    }
                    if (employeeSelect.Count > 0)
                    {
                        foreach (int employeeID in employeeSelect)
                        {
                            this.EmployeeService.UpdateDelete(employeeID, false);
                        }
                        
                    }
                    Bind();
                }
                catch (Exception ex)
                {
                    ProcessException(ex);
                }
            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void gvEmployees_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEmployees.PageIndex = e.NewPageIndex;
            this.Bind();
        }
    }
}